package com.android.controller;

import com.android.networking.connection.ClientInfoPool;
import com.android.networking.connection.Connection;
import com.android.networking.message.DataMessage;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

/**
 * Game Logic Class - Manages the game's control logic.
 * 
 * @author David Alban
 * @version 1.0 
 */
public class GameLogic
{
	/**
	 * Number of players in the game
	 */
	private int playersInGame;
	/**
	 * Number of rounds played the game
	 */
	private int rounds;
	/**
	 * Maximum round in a game
	 */
	public static final int MAX_ROUNDS = 10;
	/**
	 * Instance of round logic
	 */
	private RoundLogic roundLogic;
	/**
	 * Instance of connection
	 */
	private Connection connection;
	/**
	 * Instance of client information pool
	 */
	private static HashMap<String,ClientInfoPool> clientInfo;
	/**
	 * Collection of clients
	 */
	private Collection<ClientInfoPool> clientCollection;
	/**
	 * Array of clients
	 */
	private ClientInfoPool [] clientArray;
	/**
	 * Instance of player game information
	 */
	private PlayerGameInfo playerGameInfo;
	
	/**
	 * Constructor - Initializes the game logic class.
	 */	
	public GameLogic()
	{
		rounds = 0;
		roundLogic = new RoundLogic();
		
		clientInfo = Connection.getClientPool();
		clientCollection = clientInfo.values();
		clientArray = (ClientInfoPool [])clientCollection.toArray();

		// Update numbers of players in the game
		playersInGame = clientArray.length;
	}
	
	/**
	 * Modifier Method - Play a complete game
	 */
	public void playGame()
	{	
		// While there are rounds left in the game
		while(rounds < MAX_ROUNDS)
		{	
			// Check to see if you have at least 3 players
			if(playersInGame > 2)
			{			
				// First round
				if(rounds == 0)
				{
					// Set player 0 (server) to drawer
					playerGameInfo = clientArray[0].getPlayerGameInfo();
					playerGameInfo.setPlayerMode(false);
				}
				
				roundLogic.playRound();
				
				// Wait for round to be over
				while(!roundLogic.isRoundOver())
				{
					// Wait 10 Seconds
					wait(10);
				}
				
				// Wait for round to be complete
				while(!roundLogic.isRoundComplete())
				{
					// Wait 10 Seconds
					wait(10);
				}
			}
			else
			{
				// Wait for players to join
				waitForPlayers();
			}
		}
	}
	
	/**
	 * Modifier Method - Checks to see if there are at least 3 players in the 
	 * game to start a round. If there not at least 3 players in the game, 
	 * will wait until there are.  
	 */
	public void waitForPlayers()
	{
		// Wait for 3 players to start a round
		while(playersInGame < 3)
		{
			// Wait 10 Seconds
			wait(10);
			
			// Update the client array
			clientInfo = Connection.getClientPool();
			clientCollection = clientInfo.values();
			clientArray = (ClientInfoPool [])clientCollection.toArray();
	
			// Update numbers of players in the game
			playersInGame = clientArray.length;
		}
	}
	
	/**
	 * Special Method - Waits for a given amount of seconds. 	
	 * @param	sec		number of seconds to wait
	 */
	public void wait(int sec)
	{
		try
		{
			// Do nothing for sec seconds
			Thread.sleep(sec * 1000); 
		}
		catch(InterruptedException e)
		{
			e.printStackTrace();
		}
	}
}
